package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.ArticleLookCensusService;
import io.lkt.modules.sys.service.BBrandInfoService;
import io.lkt.modules.sys.service.KeywordDetailLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys/articlelookcensus")
public class ArticleLookCensusController extends AbstractController{
    @Autowired
    private ArticleLookCensusService articleLookCensusService;

    @Autowired
    BBrandInfoService bBrandInfoService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = articleLookCensusService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/queryList")
    public R queryList(@RequestParam Map<String, Object> params){
        if(params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }

        if(params.get("startDate")!=null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        }
        if(params.get("endDate")!=null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate")+" 23:59:59");
        }

        String brandName = (String)params.get("brandName");
        BBrandInfoEntity bBrandInfoEntity = this.bBrandInfoService.queryByName(brandName);
        if(bBrandInfoEntity!=null){
            params.put("brandId", bBrandInfoEntity.getId());
        }

        Query query = new Query(params);
        List<ArticleLookCensusEntity> list=articleLookCensusService.queryList(query);
        int total = 5000;
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page",pageUtil);
    }



    @RequestMapping("/queryMainPage")
    public R queryMainPage(@RequestBody Map<String, Object> params) {

        String startDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date());
        String endDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date()) + " 23:59:59";

        if (params.get("pDate") != null) {
            startDate = params.get("pDate").toString();
            endDate = params.get("pDate").toString() + " 23:59:59";
        }
        params.put("startDate", startDate);
        params.put("endDate", endDate);

        Map<String, Object> rMap=new HashMap<>();

        List<ArticleLookCensusEntity> listLook = articleLookCensusService.queryVitalLook(params);
        List<ArticleLookCensusEntity> listShare = articleLookCensusService.queryVitalShare(params);
        List<ArticleLookCensusEntity> listClock = articleLookCensusService.queryVitalClock(params);

        if (listLook != null && listLook.size() > 0) {
            for (ArticleLookCensusEntity phl : listLook) {
                if (phl.getResourceType().equals(1)) {
                    rMap.put("articleLook",phl.getLookCount());
                } else if (phl.getResourceType().equals(2)) {
                    rMap.put("videoLook",phl.getLookCount());
                }
            }
        }

        if (listShare != null && listShare.size() > 0) {
            for (ArticleLookCensusEntity phl : listShare) {
                if (phl.getResourceType().equals(1)) {
                    rMap.put("articleShare",phl.getShareCount());
                } else if (phl.getResourceType().equals(2)) {
                    rMap.put("videoShare",phl.getShareCount());
                }
            }
        }

        if (listClock != null && listClock.size() > 0) {
            for (ArticleLookCensusEntity phl : listClock) {
                if (phl.getResourceType().equals(1)) {
                    rMap.put("articleClock",phl.getLookCount());
                } else if (phl.getResourceType().equals(2)) {
                    rMap.put("videoClock",phl.getLookCount());
                }
            }
        }

        return R.ok().put("vital", rMap);
    }

}
